
export class EventDispatcher {
    private _map: Map<string, Array<any>> = new Map();
    on(key: string, cb: Function, listener: any, args: any) {
        let array = this._map.get(key);
        if (!array) {
            array = [];
            this._map.set(key, array);
        }
        array.push({
            cb: cb,
            listener: listener,
            args: args
        });
    }

    emit(key: string, data: any) {
        let array = this._map.get(key);
        if (!array)
            return;
        for (let i = 0; i < array.length; i++) {
            let e = array[i];
            e.cb.call(e.listener, key, data, e.args);
        }
    }

    destroy() {
        this._map.clear();
    }
}